﻿using HospitalSalarySystem.Models;
using HospitalSalarySystem.Repositories;
using HospitalSalarySystem.Services;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Linq;

namespace HospitalSalarySystem.Forms
{
    public partial class MainForm : Form
    {
        private readonly EmployeeRepository _repository;

        public MainForm()
        {
            InitializeComponent();
            // 初始化数据库仓储，数据库文件存储在应用程序目录
            _repository = new EmployeeRepository("employees.db");
            ConfigureDataGridView();
            LoadEmployees();
        }

        /// <summary>
        /// 配置DataGridView显示格式
        /// </summary>
        private void ConfigureDataGridView()
        {
            dgvEmployees.AutoGenerateColumns = false;
            dgvEmployees.AllowUserToAddRows = false;
            dgvEmployees.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

        }

        /// <summary>
        /// 加载所有员工数据
        /// </summary>
        private void LoadEmployees()
        {
            try
            {
                dgvEmployees.DataSource = _repository.GetAll();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载数据失败：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// 添加按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            using (var editForm = new EmployeeEditForm())
            {
                if (editForm.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        _repository.Insert(editForm.Employee);
                        LoadEmployees();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show($"添加失败：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

        /// <summary>
        /// 编辑按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvEmployees.SelectedRows.Count == 0) return;

            var selectedEmployee = (Employee)dgvEmployees.SelectedRows[0].DataBoundItem;
            using (var editForm = new EmployeeEditForm(selectedEmployee))
            {
                if (editForm.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        _repository.Update(editForm.Employee);
                        LoadEmployees();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show($"更新失败：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

        /// <summary>
        /// 删除按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgvEmployees.SelectedRows.Count == 0) return;

            var result = MessageBox.Show("确认删除选中员工？", "删除确认",
                MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

            if (result == DialogResult.Yes)
            {
                try
                {
                    var id = ((Employee)dgvEmployees.SelectedRows[0].DataBoundItem).Id;
                    _repository.Delete(id);
                    LoadEmployees();
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"删除失败：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        /// <summary>
        /// 计算薪资按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCalculate_Click(object sender, EventArgs e)
        {
            if (dgvEmployees.SelectedRows.Count == 0) return;

            var service = new SalaryCalculationService();
            foreach (DataGridViewRow row in dgvEmployees.SelectedRows)
            {
                var employee = (Employee)row.DataBoundItem;
                decimal increase = service.CalculateSalaryIncrease(employee);
                MessageBox.Show($"{employee.Name} 年度薪资增长：{increase:C}",
                    "计算结果",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
            }
        }

        /// <summary>
        /// 导入按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnImport_Click(object sender, EventArgs e)
        {
            using (var importForm = new ImportForm())
            {
                if (importForm.ShowDialog() == DialogResult.OK)
                {
                    LoadEmployees();
                    MessageBox.Show($"成功导入 {importForm.SuccessCount} 条记录",
                        "导入完成",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Information);
                }
            }
        }


    }
}